package com.example.wisdomclassroomapi.facade.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wisdomclassroomapi.application.service.InfoLessonCarouselApplicationService;
import com.example.wisdomclassroomapi.common.dto.PageDto;
import com.example.wisdomclassroomapi.domain.entity.InfoLessonCarouselEntity;
import com.example.wisdomclassroomapi.facade.assembler.InfoLessonCarouselFacadeAssembler;
import com.example.wisdomclassroomapi.facade.controller.common.BaseController;
import com.example.wisdomclassroomapi.facade.dto.InfoLessonCarouselRequestDto;
import com.example.wisdomclassroomapi.facade.dto.InfoLessonCarouselResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.example.wisdomclassroomapi.facade.controller.wxapp
 * @Description: <前端控制器>-相关接口
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/infoLessonCarousel")
@Slf4j
public class InfoLessonCarouselController extends BaseController {
@Resource
private InfoLessonCarouselApplicationService infoLessonCarouselApplicationService;

@Resource
private InfoLessonCarouselFacadeAssembler infoLessonCarouselFacadeAssembler;

/**
* 分页查询
*/
@PostMapping("/page")
public PageDto<List<InfoLessonCarouselResponseDto>> getInfoLessonCarouselByPage(@RequestBody @Valid InfoLessonCarouselRequestDto request) {
        IPage<InfoLessonCarouselEntity> page = new Page<InfoLessonCarouselEntity>();
        page.setSize(request.getPageSize());
        page.setCurrent(request.getPageNum());
       InfoLessonCarouselEntity entity= infoLessonCarouselFacadeAssembler.assembleInfoLessonCarouselEntity(request);
        page =  infoLessonCarouselApplicationService.page(entity, page);
        List<InfoLessonCarouselResponseDto> assembleData = new ArrayList<>();
        if (page != null&& page.getRecords()!=null&&page.getRecords().size() > 0) {
        for (InfoLessonCarouselEntity item : page.getRecords()) {
           assembleData.add(infoLessonCarouselFacadeAssembler.assembleInfoLessonCarouselResponseDto(item));
        }
        }
        PageDto<List<InfoLessonCarouselResponseDto>> result = new PageDto<>();
        result.setPageSize((int) page.getSize());
        result.setPageNum((int) page.getCurrent());
        result.setTotal(page.getTotal());
        result.setData(assembleData);
        return result;
}


/**
* 查询
*/
@PostMapping("/list")
public List<InfoLessonCarouselResponseDto> getInfoLessonCarousels(@RequestBody @Valid InfoLessonCarouselRequestDto request) {
       InfoLessonCarouselEntity entity= infoLessonCarouselFacadeAssembler.assembleInfoLessonCarouselEntity(request);
        List<InfoLessonCarouselEntity> list= infoLessonCarouselApplicationService.list(entity);
        List<InfoLessonCarouselResponseDto> assembleData = new ArrayList<>();
        if(list!=null&&list.size()>0){
           for (InfoLessonCarouselEntity item : list) {
              assembleData.add(infoLessonCarouselFacadeAssembler.assembleInfoLessonCarouselResponseDto(item));
           }
        }
        return assembleData;
}
/**
* 获取某一实体
*/
@PostMapping("/getInfoLessonCarousel")
public InfoLessonCarouselResponseDto getInfoLessonCarouselDetails(String id) {
       InfoLessonCarouselEntity entity = infoLessonCarouselApplicationService.findById(id);
       InfoLessonCarouselResponseDto responseDto=null;
        if(entity!=null){
            responseDto= infoLessonCarouselFacadeAssembler.assembleInfoLessonCarouselResponseDto(entity);
        }
        return responseDto;
}
/**
* 新增数据
*/
@PostMapping("/add")
public void saveInfoLessonCarousel(@RequestBody InfoLessonCarouselRequestDto request) {
       InfoLessonCarouselEntity entity= infoLessonCarouselFacadeAssembler.assembleInfoLessonCarouselEntity(request);
       infoLessonCarouselApplicationService.add(entity);
}
/**
* 修改数据
*/
@PostMapping("/update")
public void modifyInfoLessonCarousel(@RequestBody InfoLessonCarouselRequestDto request) {
       InfoLessonCarouselEntity entity= infoLessonCarouselFacadeAssembler.assembleInfoLessonCarouselEntity(request);
       infoLessonCarouselApplicationService.updateById(entity);
}

/**
* 删除数据
*/
@PostMapping("/delete")
public void removeInfoLessonCarousel(@RequestBody @Valid  String id) {
       infoLessonCarouselApplicationService.deleteById(id);
}
}